from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from rest_framework.permissions import IsAdminUser, IsAuthenticated

from Person.authenticate import MyJSONWebTokenAuthentication
from Robots.models import RemoteOJ
from Robots.models.serializers import RemoteOJSerializer
from Utils.BaseResponse import BaseResponse


class RoBotAPIView(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = RemoteOJ.objects.all()
    serializer_class = RemoteOJSerializer
    authentication_classes = [MyJSONWebTokenAuthentication]  # 登录认证
    permission_classes = [IsAuthenticated, IsAdminUser]

    def get(self, request, format=None):
        res = self.list(request)
        if res.status_code == 200:
            return BaseResponse(data={"list": res.data}, status=status.HTTP_200_OK).success()
        return BaseResponse(data=res.data, status=status.HTTP_400_BAD_REQUEST).fail()

    def post(self, request, format=None):
        res = self.create(request)
        if res.status_code == status.HTTP_201_CREATED:
            return BaseResponse(data=res.data, status=status.HTTP_201_CREATED).success()
        return BaseResponse(data=res.data, status=status.HTTP_400_BAD_REQUEST).fail()
